﻿<html DIR="LTR" xmlns:tool="http://www.microsoft.com/tooltip" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:ddue="http://ddue.schemas.microsoft.com/authoring/2003/5" xmlns:MSHelp="http://msdn.microsoft.com/mshelp">
  <head>
    <META HTTP-EQUIV="Content-Type" CONTENT="text/html; CHARSET=utf-8" />
    <META NAME="save" CONTENT="history" />
    <title>Setting Adaptive Buffering Sample</title>
    
    <link rel="stylesheet" type="text/css" href="../local/Classic.css">
      
    </link>
    
    <script src="../local/script.js">
      
    </script><script src="../local/script_main.js">&amp;nbsp;</script>
  </head>
  <body>
    <!--Topic built:09/24/2007 09:01:38-->

    
    
    
    
    
    
    
    
    
    <div id="header">
      <table width="100%" id="topTable"><tr>
          <td align="left">
            <span id="headerBold">Setting Adaptive Buffering Sample</span>
          </td>
          <td align="right">
            
          </td>
        </tr></table>
      
      
      
    </div>
    <div id="mainSection">
      
        
        
		<font color="DarkGray"><!-- [This topic is pre-release documentation and is subject to change in future releases. Blank topics are included as placeholders.]<br/>[Documentation built on $$TIMESTAMP$$]  --> </font> <p /> <p />
	
        <div id="introductionSection" class="section">
    <p>This Microsoft SQL Server 2005 JDBC Driver sample application demonstrates how to set the adaptive buffering mode in a connection URL and on a statement level. It also demonstrates how to retrieve a large single-column value from a SQL Server database by using the <a href="c70e210f-0288-47cc-9268-a29c45979729.htm">getCharacterStream</a> method.</p>
    <p>The code file for this sample is named useAdaptiveBuffering.java, and it can be found in the following location:</p>
    <p>&lt;<i>installation directory</i>&gt;\sqljdbc_&lt;<i>version</i>&gt;\&lt;<i>language</i>&gt;\help\samples\adaptive</p>
  </div><h1 class="heading">Requirements</h1><div id="requirementsSection" class="section">
    <content xmlns="http://ddue.schemas.microsoft.com/authoring/2003/5">
      <p xmlns="">To run this sample application, you must set the classpath to include the sqljdbc.jar file. If the classpath is missing an entry for sqljdbc.jar, the sample application will throw the common "Class not found" exception. You will also need access to the SQL Server 2005 AdventureWorks sample database. </p>
      <p xmlns="">For more information about how to set the classpath, see <a href="6faaf05b-8b70-4ed2-9b44-eee5897f1cd0.htm">Using the JDBC Driver</a>.</p>
    </content>
  </div><h1 class="heading">Example</h1><div id="codeExampleSection" class="section">
    <description xmlns="http://ddue.schemas.microsoft.com/authoring/2003/5">
      <content>
        <p xmlns="">In the following example, the sample code sets the <b>responseBuffering</b> connection property and other various connection properties in the connection URL, and then makes a connection to the SQL Server 2005 AdventureWorks database. Next, the sample code creates sample data and updates the Production.Document table by using a parameterized query. </p>
        <p xmlns="">In addition, the sample code demonstrates how to set the adaptive buffering mode by using the <a href="9f489835-6cda-4c8c-b139-079639a169cf.htm">setResponseBuffering</a> method of the <a href="ec24963c-8b51-4838-91e9-1fbfa2347451.htm">SQLServerStatement</a> class. Note that you only have to call this method if you have not already set the responseBuffering=adaptive in the connection properties.</p>
        <p xmlns="">Then, using an SQL statement with the <a href="ec24963c-8b51-4838-91e9-1fbfa2347451.htm">SQLServerStatement</a> object, the sample code runs the SQL statement and places the data that it returns into a <a href="eaffcff1-286c-459f-83da-3150778480c9.htm">SQLServerResultSet</a> object.</p>
        <p xmlns="">Finally, the sample code iterates through the rows of data that are contained in the result set, and uses the <a href="c70e210f-0288-47cc-9268-a29c45979729.htm">getCharacterStream</a> method to access some of the data that it contains. </p>
      </content>
    </description>
    <div class="sampleCode"><span codeLanguage="other"><pre>import java.sql.*;
import java.io.*;
import com.microsoft.sqlserver.jdbc.SQLServerStatement;

public class useAdaptiveBuffering {
	
   public static void main(String[] args) {

      // Create a variable for the connection string.
      String connectionUrl = 
    	    "jdbc:sqlserver://localhost:1433;" +
            "databaseName=AdventureWorks;integratedSecurity=true;" +
            "responseBuffering=adaptive";

      // Declare the JDBC objects.
      Connection con = null;
      Statement stmt = null;
      ResultSet rs = null;  
           
      try {
          // Establish the connection.
          Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
          con = DriverManager.getConnection(connectionUrl);
         
          // Create test data as an example.
          StringBuffer buffer = new StringBuffer(4000);
          for (int i = 0; i &lt; 4000; i++) 
              buffer.append( (char) ('A'));
	    	
          PreparedStatement pstmt = con.prepareStatement(
                    "UPDATE Production.Document " +
                     "SET DocumentSummary = ? WHERE (DocumentID = 1)");

          pstmt.setString(1, buffer.toString());
          pstmt.executeUpdate();
          pstmt.close();

          // In adaptive mode, the application does not have to use a server cursor 
          // to avoid OutOfMemoryError when the SELECT statement produces very large
          // results. 

          // Create and execute an SQL statement that returns some data.
          String SQL = "SELECT Title, DocumentSummary " +
	      		       "FROM Production.Document";
          stmt = con.createStatement();

          // If you have not already set the responseBuffering=adaptive in the 
          // connection properties, you can set the response buffering to adaptive 
          // on the statement level before executing the query, such as:	      
          SQLServerStatement SQLstmt = (SQLServerStatement) stmt;
          SQLstmt.setResponseBuffering("adaptive");	      
	      
          // Display the response buffering mode.
          System.out.println("Response buffering mode has been set to " +
             SQLstmt.getResponseBuffering());          
          
          // Get the updated data from the database and display it.
          rs = stmt.executeQuery(SQL);
	                  
          while (rs.next()) {
               Reader reader = rs.getCharacterStream(2);
               if (reader != null)
               {
                  char output[] = new char[40];
                  while (reader.read(output) != -1)
                  {
                      // Do something with the chunk of the data that was                       
                      // read.
                  }		 		 
 
                  System.out.println(rs.getString(1) + 
                      " has been accessed for the summary column.");
                  // Close the stream.
                  reader.close();
               }
          }
      }
      // Handle any errors that may have occurred.
      catch (Exception e) {
         e.printStackTrace();
      }
      finally {
          if (rs != null) try { rs.close(); } catch(Exception e) {}
          if (stmt != null) try { stmt.close(); } catch(Exception e) {}
    	  if (con != null) try { con.close(); } catch(Exception e) {}
      }
   }
}
</pre></span></div>
  </div><span id="seeAlsoSpan"><h1 class="heading">See Also</h1></span><div id="seeAlsoSection" class="section" name="collapseableSection"><a href="5b93569f-eceb-4f05-b49c-067564cd3c85.htm">Working with Adaptive Buffering</a><br /><br /></div><!--[if gte IE 5]>
			<tool:tip element="seeAlsoToolTip" avoidmouse="false"/><tool:tip element="languageFilterToolTip" avoidmouse="false"/><tool:tip element="roleInfoSpan" avoidmouse="false"/>
		<![endif]-->
      <div id="footer" class="section">
        
		<hr />
		
		<span id="fb" class="feedbackcss">
			
			
		</span>
		
		<a href="9bad553b-9e70-4696-8499-2e35f772a1e0.htm">
			
			© 2007 Microsoft Corporation. All rights reserved.
		</a>
 	
	
      </div>
    </div>
  </body>
</html>